<?php
/**
 * 实时库存调整（国内）
 * (model)
 */
namespace App\Models\Inventory;

use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use App\Util\User;
use Illuminate\Database\Eloquent\Model;
class Adjust extends Model
{
    protected $table = 'inventory_adjust';

    protected $guarded = [];

    public static $_reason = [
        1 => '定期盘点',
        2 => '差异调整'
    ];

    public static $_func = [
        1 => 'Check',
        2 => 'Differ'
    ];

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public static function getDownloadData($re){
        $user = [];
        foreach ($re as $row){
            $temp = [];
            $temp['psku_code'] = $row->sku->psku_code ?? '';
            $temp['season'] = ProductClassify::$_season[$row->sku->classify->season ?? 3];
            $temp['classify'] = $row->sku->classify->title ?? '';
            $temp['num'] = $row->num;
            $temp['reason'] = self::$_reason[$row->reason];
            $temp['psku_cn'] = $row->sku->psku_cn ?? '';
            if(!array_key_exists($row->auser_id,$user)){
                $user[$row->auser_id] = User::getUsernameById($row->auser_id);
            }
            $temp['auser_id'] = $user[$row->auser_id];
            $temp['created_at'] = $row->created_at;
            yield $temp;
        }
    }
}
